﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Web.Models;
using System.Data.SqlClient;
using System.Data;
using Web.Extensions;

namespace Web.Controllers
{
    public class CustomerController : Controller
    {
        //
        // GET: /Customer/

        QUANLYSIEUTHI_LVEntities dbContext = new QUANLYSIEUTHI_LVEntities();

        public ActionResult Index()
        {
            return View();
        }
        
        public JsonResult Login()
        {
            string username = Request.Params["username"];
            string password = Request.Params["password"];

            var query = from p in dbContext.KHACHHANGs
                        where p.ID_TKKH == username && p.MatKhau_KH == password
                        select p;
            var json = true;
            if (query.Count() > 0)
            {
                
                Session["user"] = query.First();
            }
            else
            {
                json = false;
            }

            
            
            return Json(json);
        }

        public ActionResult Logout() {
            Session.Clear();
            return Redirect("/Home/Index");

        }
       
        public int Add()
        {

            KHACHHANG khachhang = new KHACHHANG();
            //khachhang.MaKH=HttpContext.Request.QueryString[ "makh" ].ToString();
            khachhang.MaKH = khachhang.GenerateID();
            khachhang.TenKH = HttpContext.Request.QueryString["tenkh"].ToString();
            khachhang.NamSinhKH = DateTime.Parse(HttpContext.Request.QueryString["namsinh"].ToString());
            khachhang.SDT_KH = HttpContext.Request.QueryString["sdt"].ToString();
            khachhang.CMND = int.Parse(HttpContext.Request.QueryString["cmnd"].ToString());
            khachhang.ID_TKKH = HttpContext.Request.QueryString["idkh"].ToString();
            khachhang.MatKhau_KH = HttpContext.Request.QueryString["matkhau"].ToString();
            khachhang.DiaChiKH = HttpContext.Request.QueryString["diachi"].ToString();
              //khachhang. = HttpContext.Request.QueryString["diachi"].ToString();
            dbContext.KHACHHANGs.AddObject(khachhang);
            return dbContext.SaveChanges();
        }

        

        public ActionResult Register()
        {
            return View("_Get_Register");
        }

    }
}
